package com.hp.appfw.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class TimeUtil {
	public static String timeFormat(Date d, String fm, String tzs)
	{
		if(d == null || fm == null)
			return "";
		SimpleDateFormat time=new SimpleDateFormat(fm);
		
		if(tzs != null)
		{
			TimeZone tz = TimeZone.getTimeZone(tzs);
			time.setTimeZone(tz);
		}
		return time.format(d);
	}

	public static Date timeParse(String d, String fm, String tzs)
	{
		if(d == null || fm == null)
			return null;

		try {
			SimpleDateFormat time=new SimpleDateFormat(fm);
			if(tzs != null)
			{
				TimeZone tz = TimeZone.getTimeZone(tzs);
				time.setTimeZone(tz);
			}
			return time.parse(d);
		} catch (ParseException e) {
			return null;
		}
	}
	public static String timeFormat(Date d, String tzs)
	{
		String fm = "yyyy-MM-dd HH:mm:ss";
		if(d == null || fm == null)
			return "";
		SimpleDateFormat time=new SimpleDateFormat(fm);
		if(tzs != null)
		{
			TimeZone tz = TimeZone.getTimeZone(tzs);
			time.setTimeZone(tz);
		}
		return time.format(d);
	}

	public static Date timeParse(String d, String tzs)
	{
		String fm = "yyyy-MM-dd HH:mm:ss";

		if(d == null || fm == null)
			return null;
		try {
			SimpleDateFormat time=new SimpleDateFormat(fm);
			if(tzs != null)
			{
				TimeZone tz = TimeZone.getTimeZone(tzs);
				time.setTimeZone(tz);
			}
			return time.parse(d);
		} catch (ParseException e) {
			return null;
		}
	}
	public static String microTimeFormat(Date d, String tzs)
	{
		String fm = "yyyyMMddHHmmssSSS";
		if(d == null || fm == null)
			return "";
		SimpleDateFormat time=new SimpleDateFormat(fm);
		if(tzs != null)
		{
			TimeZone tz = TimeZone.getTimeZone(tzs);
			time.setTimeZone(tz);
		}
		return time.format(d);
	}

	public static Date microTimeParse(String d, String tzs)
	{
		String fm = "yyyyMMddHHmmssSSS";

		if(d == null || fm == null)
			return null;
		try {
			SimpleDateFormat time=new SimpleDateFormat(fm);
			if(tzs != null)
			{
				TimeZone tz = TimeZone.getTimeZone(tzs);
				time.setTimeZone(tz);
			}
			return time.parse(d);
		} catch (ParseException e) {
			return null;
		}
	}
}
